package com.specialtreasure.freightdepot.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.app.core.config.Constants;
import com.app.core.config.HttpApi;
import com.app.core.fragment.BaseNewFragment;
import com.app.core.util.UserUtils;
import com.app.core.view.MarqueeTextView;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.activity.LoginActivity;
import com.specialtreasure.freightdepot.activity.MyOrderActivity;
import com.specialtreasure.freightdepot.activity.MyOwnerActivity;
import com.specialtreasure.freightdepot.activity.MyTradeActivity;
import com.specialtreasure.freightdepot.model.BaseModel;
import com.specialtreasure.freightdepot.model.ZUserResponse;
import com.vondear.rxtools.view.dialog.RxDialogSureCancel;
import com.yanzhenjie.httpmodule.http.CallServer;
import com.yanzhenjie.httpmodule.http.Result;
import com.yanzhenjie.httpmodule.http.StringRequest;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.CacheMode;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.List;

/**
 * Created by Administrator on 2017/8/7 0007.
 * 我的页面
 */

public class MeFragment extends BaseNewFragment implements View.OnClickListener {

    public static final String TAG = "LoginActivity";
    private TextView tv_title;
    private TextView tv_sure;
    private TextView tv_name;
    private TextView tv_tel;
    private TextView tv_companyname;
    private MarqueeTextView tv_navigation;
    private RelativeLayout rl_order;
    private RelativeLayout rl_peer;
    private RelativeLayout rl_owner;
    private TextView tv_order_num;
    private TextView tv_peer_num;
    private TextView tv_owner_num;
    private RelativeLayout rl_exit;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(mContext, R.layout.frag_me, null);
        initView(view);
        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
        setData();
        showDialog(Constants.LOADING);
        getData();
        getAD();
    }

    private void setData() {
        tv_title.setText("我的");
        show(tv_sure);
        tv_sure.setText("注销");
        tv_sure.setTextColor(mContext.getResources().getColor(R.color.common_FD4141));


//        List<String> adList = new ArrayList<>();
//        adList.add("广告");
//        tv_navigation.init(adList);

    }


    /**
     * 网络请求
     */
    private void getData() {
        String url = Constants.HTTP_URL + HttpApi.MYDATA;
        StringRequest request = new StringRequest(url, RequestMethod.POST);
        request.add("token", UserUtils.getToken());

        request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);

        CallServer.getInstance().request(Constants.ZERO, request, new SimpleResponseListener<Result<String>>() {
            @Override
            public void onSucceed(int what, Response<Result<String>> response) {
                super.onSucceed(what, response);
                closeDialog();

                String result = response.get().getResult();
                Logger.e("请求成功：" + result);

                JSONObject jsonObject = JSON.parseObject(result);
                if (jsonObject != null) {
                    Integer status = jsonObject.getInteger("status");
                    String msg = jsonObject.getString("msg");

                    if (status != null && status == 0) {
                        ZUserResponse data = JSON.parseObject(jsonObject.getJSONObject("data").toString(), ZUserResponse.class);
                        if (data != null) {
                            tv_name.setText(data.getRealName());
                            tv_tel.setText(data.getPhoneNumber());
                            tv_order_num.setText(data.getOrderNum());
                            tv_peer_num.setText(data.getSameNum());
                            tv_owner_num.setText(data.getGoodsUserNum());
                            tv_companyname.setText(data.getCompanyName());
                        }

                    } else {
                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();

                    }

                }

            }


        });
    }

    /**
     * 获取广告
     */
    private void getAD(){
        String url = Constants.HTTP_URL + HttpApi.ADVERTLIST;
        StringRequest request = new StringRequest(url, RequestMethod.POST);

        request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);

        CallServer.getInstance().request(Constants.ONE, request, new SimpleResponseListener<Result<String>>() {
            @Override
            public void onSucceed(int what, Response<Result<String>> response) {
                super.onSucceed(what, response);
                closeDialog();

                String result = response.get().getResult();
                Logger.e("请求成功：" + result);

                JSONObject jsonObject = JSON.parseObject(result);
                if (jsonObject != null) {
                    Integer status = jsonObject.getInteger("status");
                    String msg = jsonObject.getString("msg");

                    if (status != null && status == 0) {
                        List<String> data = JSON.parseArray(jsonObject.getJSONArray("data").toString(), String.class);
                        if (data != null&& data.size()>0) {
                            tv_navigation.init(data);
                        }

                    } else {
                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();

                    }

                }

            }


        });
    }





    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_order://我的订单
                intent.setClass(mContext, MyOrderActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_peer://我的同行
                intent.setClass(mContext, MyTradeActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_owner://我的货主
                intent.setClass(mContext, MyOwnerActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_sure://退出
                logout();
                break;
        }
    }

    /**
     * 注销登录
     */
    private void logout() {
        final RxDialogSureCancel dialogSureCancel = new RxDialogSureCancel(getActivity());
        dialogSureCancel.setTitle("专线宝");
        dialogSureCancel.getTvTitle().setBackgroundDrawable(null);
        dialogSureCancel.setCanceledOnTouchOutside(false);
        dialogSureCancel.show();
        dialogSureCancel.setSureListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent.setClass(mContext, LoginActivity.class);
                startActivity(intent);
                UserUtils.logout();
                dialogSureCancel.dismiss();
                getActivity().finish();
            }
        });

        dialogSureCancel.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogSureCancel.dismiss();
            }
        });
    }


    private void initView(View view) {

        tv_navigation = (MarqueeTextView) view.findViewById(R.id.tv_navigation);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_name = (TextView) view.findViewById(R.id.tv_name);
        tv_tel = (TextView) view.findViewById(R.id.tv_tel);
        tv_companyname = (TextView) view.findViewById(R.id.tv_companyname);
        rl_order = (RelativeLayout) view.findViewById(R.id.rl_order);
        rl_order.setOnClickListener(this);
        rl_peer = (RelativeLayout) view.findViewById(R.id.rl_peer);
        rl_peer.setOnClickListener(this);
        rl_owner = (RelativeLayout) view.findViewById(R.id.rl_owner);
        rl_owner.setOnClickListener(this);
        tv_order_num = (TextView) view.findViewById(R.id.tv_order_num);
        tv_order_num.setOnClickListener(this);
        tv_peer_num = (TextView) view.findViewById(R.id.tv_peer_num);
        tv_peer_num.setOnClickListener(this);
        tv_owner_num = (TextView) view.findViewById(R.id.tv_owner_num);
        tv_owner_num.setOnClickListener(this);
        rl_exit = (RelativeLayout) view.findViewById(R.id.rl_exit);
        rl_exit.setOnClickListener(this);
        tv_sure = (TextView) view.findViewById(R.id.tv_sure);
        tv_sure.setOnClickListener(this);
    }


    @Subscriber(tag = Constants.UPDATEUSER)
    private void updateData(BaseModel baseModel) {
        getData();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        // 注销
        EventBus.getDefault().unregister(this);
    }


    @Override
    protected String getFragmentName() {
        return TAG;
    }

}
